package com.rfsp.mcp.controller.hos;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rfsp.common.util.XssUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rfsp.common.base.ResultVO;
import com.rfsp.common.log.TrcMsg;
import com.rfsp.common.shiro.ShiroUtils;
import com.rfsp.common.util.FileUtil;
import com.rfsp.mcp.util.McpPdfUtil;
import com.rfsp.common.util.StringUtil;
import com.rfsp.common.pojo.po.OrgPO;
import com.rfsp.mcp.pojo.po.OrgYbjPO;
import com.rfsp.mcp.pojo.vo.HospitalPdfVO;
import com.rfsp.mcp.pojo.vo.HospitalPdfVO2;
import com.rfsp.mcp.pojo.vo.HospitalVO;
import com.rfsp.mcp.pojo.vo.PfmVO;
import com.rfsp.mcp.service.OrgYbjService;

@RequiresRoles(value={"mcp_hos"}, logical= Logical.AND)
@Controller("HosHospitalController")
@RequestMapping("/hos/hospital")
public class HosHospitalController extends HosController {
    public Logger logger = LoggerFactory.getLogger(HosHospitalController.class);

    @Value("${mcp.file-temp-path-hospital}")
    private String hospitalPdfPath;

    @Autowired
    private OrgYbjService orgYbjService;

    @GetMapping("/")
    public String index(ModelMap model) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return detail(model);
    }
    
    @GetMapping("/detail")
    public String detail(ModelMap model) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "BaseHospitalController.detail"));
        
        PfmVO pfm = ShiroUtils.getAttribute("mcp_pfm");
        String itf_type = pfm.getItfType();  //0：宿迁；1：抚顺；2江浙；9其他
        OrgYbjPO ybjPO = ShiroUtils.getAttribute("org_ybj");

        if(itf_type.equals("0")) {
        	ResultVO<HospitalVO> vo = ResultVO.success(new HospitalVO());
            model.put("vo", vo);
            OrgPO org = ShiroUtils.getAttribute("org");

            //OrgYbjPO ybjPO = orgYbjService.getYbjId(org.getId(), pfm.getId());
            model.put("id",ybjPO.getId());
            model.put("aid",ybjPO.getAid());
            model.put("pdfPath","/mcp/hos/hospital/pdf/?id="+ybjPO.getId()+"&aid="+ybjPO.getAid());

            //OrgPO currentOrg = ShiroUtils.getCurrentOrg();
            //OrgYbjPO orgYbjPO = orgYbjService.getYbjId(currentOrg.getId(), pfm.getId());
            //HospitalPdfVO pdfVO = orgYbjService.getHospitalPdfVO(orgYbjPO.getId(), orgYbjPO.getAid());
            HospitalPdfVO pdfVO = orgYbjService.getHospitalPdfVO(ybjPO.getId(), ybjPO.getAid());
            XssUtils.copyProperties(pdfVO, pdfVO);
            model.put("vo",pdfVO);

            logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "BaseHospitalController.detail"));
            return "hos/hospital/detail";
        }else if(itf_type.equals("1")){
        	ResultVO<HospitalVO> vo = ResultVO.success(new HospitalVO());
            model.put("vo", vo);
            OrgPO org = ShiroUtils.getAttribute("org");

            //OrgYbjPO ybjPO = orgYbjService.getYbjId(org.getId(), pfm.getId());
            model.put("id",ybjPO.getId());
            model.put("aid",ybjPO.getAid());
            model.put("pdfPath","/mcp/hos/hospital/pdf/?id="+ybjPO.getId()+"&aid="+ybjPO.getAid());

            //OrgPO currentOrg = ShiroUtils.getCurrentOrg();
            //OrgYbjPO orgYbjPO = orgYbjService.getYbjId(currentOrg.getId(), pfm.getId());
            //HospitalPdfVO2 pdfVO = orgYbjService.getHospitalPdfVO2(orgYbjPO.getId(), orgYbjPO.getAid());
            HospitalPdfVO2 pdfVO = orgYbjService.getHospitalPdfVO2(ybjPO.getId(), ybjPO.getAid());
            XssUtils.copyProperties(pdfVO, pdfVO);
            model.put("vo",pdfVO);

            logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "BaseHospitalController.detail"));
            return "hos/hospital/detail2";
        }
        return "";
    }

    @GetMapping("/pdf")
    public void pdf(ModelMap model, Long id, String aid, HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {

        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "BaseHospitalController.pdf"));
        ResultVO<HospitalVO> vo = ResultVO.success(new HospitalVO());
        model.put("vo", vo);

//        OrgPO currentOrg = ShiroUtils.getCurrentOrg();
//        OrgYbjPO orgYbjPO = orgYbjSerivce.getYbjId(currentOrg.getId());
        
        Map<String,Object> dataMap = orgYbjService.getHospitalPdfMap(id, aid);
        
        String dirPath = FileUtil.getRootPath(hospitalPdfPath).replaceFirst("date", StringUtil.dateToString(new Date(), "yyyy/MM/dd"));
        
        try {
        	dirPath = McpPdfUtil.buildMiInfoPDF(dataMap, dirPath);
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        
        logger.info("生成预览文件成功");
        request.getSession().setAttribute(id+"hosPdfPath",dirPath);
        File file = new File(dirPath);
        FileInputStream fileInputStream = new FileInputStream(file);
        response.setHeader("Content-Type", "application/pdf");
        OutputStream outputStream = response.getOutputStream();
        IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "BaseHospitalController.pdf"));
    }

    @RequestMapping("/download")
    public String download(HttpServletRequest request, HttpServletResponse response, ModelMap model,Long id, String aid) {


        OrgYbjPO ybjPO = orgYbjService.getOrgYbj(id,aid);
        String dirPath = (String) request.getSession().getAttribute(id+"hosPdfPath");
        try {
            FileUtil.downloadfile(request, response, dirPath, ybjPO.getOrgCode() +".pdf");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}
